package cn.easydroid.android.viewhelp;

import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import cn.easydroid.android.log.XLog;

/**
 * Create Date: 2019/12/10
 * Author: Yang Du
 * Description:
 */
public class MarginPaddingHelp {

    private static String TAG = "MarginPaddingHelp";
    private View view;
    private int left;
    private int right;
    private int bottom;
    private int top;

    private MarginPaddingHelp(View view) {
        this.view = view;
    }

    public static MarginPaddingHelp newInstance(View view){
        return new MarginPaddingHelp(view);
    }

    public MarginPaddingHelp left(int left) {
        this.left = left;
        return this;
    }

    public MarginPaddingHelp right(int right) {
        this.right = right;
        return this;
    }

    public MarginPaddingHelp top(int top) {
        this.top = top;
        return this;
    }

    public MarginPaddingHelp bottom(int bottom) {
        this.bottom = bottom;
        return this;
    }

    public void setMargin() {
        setMargin(left, right, top, bottom, view);
    }

    public static void setMargin(int left, int right, int top, int bottom, View view) {
        if (view == null) {
            return;
        }
        ViewGroup.LayoutParams layoutParams = view.getLayoutParams();
        XLog.d(TAG, "set margin: "+left+", "+right+", "+top+", "+bottom+" , "+layoutParams.getClass());


        if (layoutParams instanceof RelativeLayout.LayoutParams) {
            ((RelativeLayout.LayoutParams) layoutParams).leftMargin = left;
            ((RelativeLayout.LayoutParams) layoutParams).rightMargin = right;
            ((RelativeLayout.LayoutParams) layoutParams).topMargin = top;
            ((RelativeLayout.LayoutParams) layoutParams).bottomMargin = bottom;
        } else if (layoutParams instanceof LinearLayout.LayoutParams) {
            ((LinearLayout.LayoutParams) layoutParams).leftMargin = left;
            ((LinearLayout.LayoutParams) layoutParams).rightMargin = right;
            ((LinearLayout.LayoutParams) layoutParams).topMargin = top;
            ((LinearLayout.LayoutParams) layoutParams).bottomMargin = bottom;
        } else if (layoutParams instanceof ConstraintLayout.LayoutParams) {
            ((ConstraintLayout.LayoutParams) layoutParams).leftMargin = left;
            ((ConstraintLayout.LayoutParams) layoutParams).rightMargin = right;
            ((ConstraintLayout.LayoutParams) layoutParams).topMargin = top;
            ((ConstraintLayout.LayoutParams) layoutParams).bottomMargin = bottom;
        } else if (layoutParams instanceof FrameLayout.LayoutParams) {
            ((FrameLayout.LayoutParams) layoutParams).leftMargin = left;
            ((FrameLayout.LayoutParams) layoutParams).rightMargin = right;
            ((FrameLayout.LayoutParams) layoutParams).topMargin = top;
            ((FrameLayout.LayoutParams) layoutParams).bottomMargin = bottom;
        }
        view.setLayoutParams(layoutParams);
    }

}
